package pro.shuangxi.model.manga.controller;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pro.shuangxi.common.vo.Result;
import pro.shuangxi.model.manga.vo.MagaItem;
import pro.shuangxi.model.manga.vo.MagaListItem;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：mengshx
 * @date ：Created in 2022/2/26 16:13
 * @description：https://m.100fanwo.com
 */
@RestController
@RequestMapping("manga360")
public class Manga360Controller {
    @RequestMapping("getmangaList.do")
    public Result getCatalogList(HttpServletRequest request) throws Exception {
        //https://m.100fanwo.com/rank/
        Document root = Jsoup.connect("https://m.100fanwo.com/rank/").get();
        Elements updateList = root.getElementsByClass("itemBox");
        Map<String, Object> resp = new HashMap();
        List<MagaListItem> itemList = new ArrayList<>();
        Pattern pattern = Pattern.compile("https://m.100fanwo.com/manhua/([a-z]+)/");
        for (int i = 0; i < updateList.size(); i++) {
            MagaListItem item = new MagaListItem();
            Element element = updateList.get(i);
            String imgUrl = element.getElementsByTag("mip-img").attr("src");
            String title = element.getElementsByClass("title").text();
            String key = element.getElementsByClass("title").attr("href");
            Matcher matcher = pattern.matcher(key);
            if (matcher.find()) {
                key = matcher.group(1);
            }
            String date = element.getElementsByClass("date").text();
            item.setKey(key);
            item.setId(i);
            item.setImg(imgUrl);
            item.setTitle(title);
            item.setDate(date);
            itemList.add(item);
        }
        resp.put("page", 1);
        resp.put("per_page", itemList.size());
        resp.put("total", itemList.size());
        resp.put("total_pages", 1);
        resp.put("data", itemList);
        return Result.OK(resp);
    }
    @RequestMapping("getmangaInfo.do")
    public Result getmangaInfo(String key) throws Exception {
        Document root = Jsoup.connect("https://m.100fanwo.com/manhua/"+key+"/").get();
        Elements list = root.getElementsByClass("list").get(0).getElementsByTag("li");
        Map<String, Object> resp = new HashMap();
        List<MagaItem> itemList = new ArrayList<>();
        Pattern pattern = Pattern.compile("/manhua/(.+)\\.html");
        for (int i = 0; i < list.size(); i++) {
            MagaItem item = new MagaItem();
            Element element = list.get(i);
            String title = element.getElementsByTag("a").text();
            String pathKey = element.getElementsByTag("a").attr("href");
            Matcher matcher = pattern.matcher(pathKey);
            if (matcher.find()) {
                pathKey=matcher.group(1);
            }
            item.setKey(pathKey);
            item.setId(i);
            item.setTitle(title);
            itemList.add(item);
        }
        resp.put("title", root.getElementsByClass("title").text());
        resp.put("data", itemList);
        return Result.OK(resp);
    }
}
